package com.hzncc.flowable_diboot.custom_field.controller;

import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.file.util.HttpHelper;
import com.hzncc.flowable_diboot.custom_field.service.CustomTableCommonService;
import jakarta.annotation.Nullable;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/custom-table")
@RequiredArgsConstructor
public class CustomTableCommonController {

    private final CustomTableCommonService customTableCommonService;

    /**
     * 通用查询
     * @param param
     * @param code
     * @param pagination
     * @return
     * @throws ClassNotFoundException
     */
    @GetMapping("/common-select")
    public JsonResult<?> commonSelect(@RequestParam @Nullable Map<String,Object> param, @RequestParam(value = "table-config-code") String code, Pagination pagination) throws ClassNotFoundException {
        List<?> objects = customTableCommonService.commonSelect(code, param, pagination,true);
        return JsonResult.OK(objects).bindPagination(pagination);
    }


    /**
     * 通用导出
     * @param param
     * @param code
     * @param pagination
     * @param response
     * @return
     * @throws ClassNotFoundException
     */
    @GetMapping("/common-export")
    public JsonResult<?> exportExcel(@RequestParam Map<String,Object> param, @RequestParam(value = "code") String code, Pagination pagination,Boolean pageExport, HttpServletResponse response) throws Exception {
        pageExport = pageExport != null && pageExport;
        File file = customTableCommonService.exportExcel(param, code, pagination, pageExport);
        HttpHelper.downloadLocalFile(file, file.getName(), response);
        return null;
    }
}
